package com.Main.faislamovers;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ToggleButton;

public class Signup extends Activity  {

	SharedPreferences prefs;
	
	String NAMESPACE="http://tempuri.org/";
	String METHODNAME="signup";
	String url="http://10.0.2.2:53512/Service1.asmx";
	String SopaAction="http://tempuri.org/signup";
	
	Button btn_submit;
	Spinner spinner;
	Boolean sex;
	ToggleButton toggle_btn;
	EditText edt_txt_full_name,edt_user_name,edt_password,edt_phn_numb,edt_user_email;
	String txt_edt_full_name,txt_edt_user_name,txt_edt_password,txt_edt_phn_numb,txt_spinner,txt_edt_email;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup);
		
		prefs = getSharedPreferences(AppConstants.PREFERENCES_NAME, 0);
		
		
		btn_submit = (Button) findViewById(R.id.btn_submit_xml);
		
		edt_txt_full_name = (EditText) findViewById(R.id.edt_full_name);
		edt_user_name = (EditText) findViewById(R.id.edt_user_name);
		edt_password = (EditText) findViewById(R.id.edt_password);
		edt_phn_numb = (EditText) findViewById(R.id.edt_phn_numb);
        toggle_btn = (ToggleButton) findViewById(R.id.tgl_btn_sex);
		spinner = (Spinner) findViewById(R.id.spinner_signup);
		edt_user_email=(EditText) findViewById(R.id.edt_user_email);

		    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	        this, R.array.mnth_array, android.R.layout.simple_spinner_item);
	        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        spinner.setAdapter(adapter);
	
	        btn_submit.setOnClickListener(new OnClickListener() {
				
	    				@Override
	    				public void onClick(View v) {
	    			
	    				
	    					try{
	    						
	    					txt_edt_full_name =edt_txt_full_name.getText().toString();
	    					txt_edt_user_name=edt_user_name.getText().toString();
	    					txt_edt_password=edt_password.getText().toString();
	    					txt_edt_phn_numb= edt_phn_numb.getText().toString();
	    					txt_edt_email=edt_user_email.getText().toString();
	    					txt_spinner= spinner.getSelectedItem().toString();
	    					
	    					toggle_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    							
    							@Override
    							public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    							
    								
    								if(isChecked)
    								{
    									sex = true;  //male 
    								}
    								
    								else
    								{
    									sex = false ; //FEMale
    								}
    							}
    						});
    					
	    					
	    					if (txt_edt_full_name.equals("")|| txt_edt_full_name.equals(null)) 
							{
								Toast.makeText(getBaseContext(),"Sorry User Full name is required!",Toast.LENGTH_LONG).show();
							} 
							
							else if (txt_edt_user_name.equals("")|| txt_edt_user_name.equals(null)) {
								Toast.makeText(getBaseContext(),"Sorry User name is required!", Toast.LENGTH_LONG).show();
							}
	    					
							else if (txt_edt_password.equals("")|| txt_edt_password.equals(null)) 
							{
								Toast.makeText(getBaseContext(),"Sorry User Password is required!",Toast.LENGTH_LONG).show();
							} 
							
							else if (txt_edt_phn_numb.equals("")|| txt_edt_phn_numb.equals(null)) {
								Toast.makeText(getBaseContext(),"Sorry User Mobile is required!", Toast.LENGTH_LONG).show();
							}
	    					
							else
							{
								
	    					
	    					if(setcustMsisdn())
	    					{
	    					
	    					startActivity(new Intent(Signup.this , Login.class));
	    					
	    					}
	    					
	    					else
	    					{
	    						
	    						 Toast.makeText(Signup.this,
	    							 	   "Try Again", Toast.LENGTH_LONG).show();
	    					}
							}
	    					
						
	    					}
	    					catch(Exception e)
	    					{
	    						
	    						
	    					}
	    					
	    					
	    
	    					}
	    			});
	      
       
	                
	}
	
	
	public boolean setcustMsisdn() throws IOException, XmlPullParserException {
		   
		   SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
		   request.addProperty("full_name",txt_edt_full_name );
		   request.addProperty("user_name", txt_edt_user_name);
		   request.addProperty("password", txt_edt_password);
		   request.addProperty("mobile_number", txt_edt_phn_numb);
		   request.addProperty("sex","true");
		   request.addProperty("age", txt_spinner);
		   request.addProperty("email_id", txt_edt_email);
		   
		  
		   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		   envelope.dotNet = true;
		   MarshalBase64 m = new MarshalBase64();
		   m.register(envelope);
		   envelope.encodingStyle = SoapEnvelope.ENC;
		   // send request
		   envelope.setOutputSoapObject(request);
		   HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
		   androidHttpTransport.call(SopaAction, envelope);
		   // get response
		  
		   String result = envelope.getResponse().toString();
		   
		   Editor editior = prefs.edit();
		   editior.putString(AppConstants.USER_ID, result);
		   editior.commit();

		   
		   if (result.equals("false")) {
			   Toast.makeText(Signup.this,
				 	   "SignUp Failed", Toast.LENGTH_LONG).show();
		    return false;
		   } 
		   else {
			   Toast.makeText(Signup.this,
				 	   "Signup Success", Toast.LENGTH_LONG).show();
		    return true;
		   }

		  }
	
}